package com.api.gbhg.xxd.api;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.api.gbhg.luoao.tool.ToolCURD;
import jxl.read.biff.Record;
import org.omg.CORBA.OBJECT_NOT_EXIST;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出纳支付
 */
@Path("/TheCashierToPay")
public class TheCashierToPay {

    private static final Logger logger = LoggerFactory.getLogger("gbhg");

    @POST
    @Path("/List")
    @Produces(MediaType.APPLICATION_JSON)
    public String list(Map<String,Object> mapParam){
//        logger.info("==========进入/List接口、getlist方法==========");
        String sqrq = mapParam.get("sqrq").toString();  //年份
        String departmentId = mapParam.get("departmentId").toString();  //单位
        String startTime = mapParam.get("startTime").toString();    //开始时间
        String stopTime = mapParam.get("stopTime").toString();      //结束时间
        String startperform = mapParam.get("startperform").toString();  //单号

        //出纳支付
        RecordSet filter = this.getRequestId();
        List<Map<String, Object>> list = new ArrayList<>();
//        logger.info("==========进入while循环==========");
        while(filter.next()){
            Map<String,Object> mapDate = new HashMap<>();
            String tableName = tablefname(filter.getString("requestid"));
            String requestId = filter.getString("requestid");
            //主表数据
            RecordSet rs = getPrimaytable(tableName,requestId,sqrq,departmentId);
            if(rs.getStringNoTrim("requestId").equals("")){
                continue;
            }
            mapDate.put("lastname",rs.getString("lastname"));   //经办人
            mapDate.put("sqrq",rs.getString("sqrq"));   //提单日期
            mapDate.put("lcbh",rs.getString("lcbh"));   //报销单编号
            mapDate.put("sqbxjehj",rs.getString("sqbxjehj"));   //报销金额
            if(!rs.getStringNoTrim("requestId").isEmpty()) {
                mapDate.put("lczt", ToolCURD.Processnode(rs.getStringNoTrim("requestId")));//审核状态
            }
            StringBuffer sb = new StringBuffer();
            sb.append("SELECT se.requestid,se.requestname,zh.tzdh FROM workflow_requestbase se " +
                    "INNER JOIN uf_szxtdzh zh on se.requestid = zh.bxlc " +
                    "INNER JOIN hrmresource ce on se.creater = ce.id where se.requestid = " + rs.getString("requestid")+" ");
            if(departmentId != null && !departmentId.isEmpty()){
                sb.append(" AND ce.departmentid = '"+departmentId+"'");
            }
            if(startTime != null && !startTime.isEmpty() && stopTime != null && !stopTime.isEmpty()){
                sb.append(" AND zh.modedatacreatedate BETWEEN  '"+startTime+"' AND '"+stopTime+"'");
            }
            if(startperform != null && !startperform.isEmpty()){
                sb.append(" AND zh.tzdh like  '%"+startperform+"%'");
            }
            rs.execute(sb.toString());
            if(!rs.next()){
                continue;
            }
            mapDate.put("requestid",rs.getString("requestid")); //流程id
            mapDate.put("requestname",rs.getString("requestname")); //流程标题
            mapDate.put("tzdh",rs.getString("tzdh"));   //通知单号
            list.add(mapDate);
        }
        return JSON.toJSONString(list);
    }

    /**
     * 查询主表数据
     *
     * @param tablefname 表名
     * @param requestId  流程id
     * @return
     */
    public  RecordSet getPrimaytable(String tablefname, String requestId,String sqrq,String departmentId) {
        //根据不同流程用不同的合计金额字段
        RecordSet getColumn1 = new RecordSet();
        RecordSet getColumn2 = new RecordSet();
        RecordSet getColumn3 = new RecordSet();
        RecordSet getColumn4 = new RecordSet();
        getColumn1.execute("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='"+tablefname+"' and COLUMN_NAME='sqbxjehj'");
        boolean b1 = getColumn1.next();
        getColumn2.execute("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='"+tablefname+"' and COLUMN_NAME='bxje'");
        boolean b2 = getColumn2.next();
        getColumn3.execute("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='"+tablefname+"' and COLUMN_NAME='sqbxje'");
        boolean b3 = getColumn3.next();
        getColumn4.execute("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='"+tablefname+"' and COLUMN_NAME='sqbxhj'");
        boolean b4 = getColumn4.next();
        String hjzd = "";
        if(b1){
            hjzd = "sqbxjehj";
        }
        if(b2){
            hjzd = "bxje";
        }
        if(b3){
            hjzd = "sqbxje";
        }
        if(b4){
            hjzd = "sqbxhj";
        }
        StringBuffer sb = new StringBuffer();
        sb.append("select f.requestId,lastname,sqrq,lcbh,"+hjzd+" as sqbxjehj from "+tablefname+" f,hrmresource h ,workflow_requestbase wr where f.jbr = h.id  and f.requestid = wr.requestid and wr.currentnodetype=3 and f.requestid = "+requestId+" ");
        if(sqrq != null && !sqrq.isEmpty()){
            sb.append(" AND YEAR(sqrq) = '"+sqrq+"'");
        }
        if(departmentId != null && !departmentId.isEmpty()) {
            sb.append(getAndSql("departmentid", departmentId));
        }
        RecordSet rs = new RecordSet();
        rs.executeQuery(sb.toString());
        rs.next();
        return rs;
    }

    //查询表名
    public static String tablefname(String requestId){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select  l.tablename,s.id  from workflow_requestbase r,workflow_base s,workflow_bill l where\n" +
                "r.workflowid = s.id and s.formid = l.id and  r.requestid ="+requestId);
        rs.next();
        return  rs.getStringNoTrim("tablename");
    }



    /**
     * 通知单表获取requestid
     * @return 返回requestid
     */
    public RecordSet getRequestId(){
        String sql = "SELECT se.requestid FROM workflow_requestbase se\n" +
                "INNER JOIN uf_szxtdzh zh on se.requestid = zh.bxlc\n" +
                "INNER JOIN hrmresource ce on se.creater = ce.id";

//        logger.info("通知单表获取requestidSql:"+sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        return rs;
    }

    /**
     * 生成 AND key=value  sql片段 ，当value为空或者null 返回空字符串
     * @param key
     * @param value
     * @return
     */
    private String getAndSql(String key, String value) {
        if (value != null && !value.isEmpty()) { return " AND " + key + "='" + value + "' ";
        }
        return "";
    }

}
